import { createRouter, createWebHashHistory } from 'vue-router'
import useUserStore from '@/store/user.js'

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    name: '登录',
  },
  {
    path: '/home',
    component: () => import('@/views/home/index.vue'),
    name: '首页',
    redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: '首页',
        component: () => import('@/views/dashboard/index.vue'),
      },
      {
        path: '/class',
        name: '班级管理',
        children: [
          {
            path: '/class',
            component: () => import('@/views/class/index.vue'),
            name: '班级管理',
          },
          {
            path: 'detail',
            component: () => import('@/views/class/detail.vue'),
            name: '班级详情',
          },
          {
            path: 'add',
            component: () => import('@/views/class/add.vue'),
            name: '班级新增',
          },

          {
            path: 'batch',
            component: () => import('@/views/class/batchAdd.vue'),
            name: '班级批量新增',
          },
        ],
      },
      {
        path: '/score',
        name: '成绩管理',
        children: [
          {
            path: '/score',
            component: () => import('@/views/score/index.vue'),
            name: '成绩管理',
          },
          {
            path: 'detail',
            component: () => import('@/views/score/detail.vue'),
            name: '成绩详情',
          },
          {
            path: 'add',
            component: () => import('@/views/score/add.vue'),
            name: '成绩新增',
          },
          {
            path: 'batch',
            component: () => import('@/views/score/batchAdd.vue'),
            name: '成绩批量新增',
          },
        ],
      },
      {
        path: '/student',
        name: '学生信息管理',
        children: [
          {
            path: '/student',
            name: '学生信息管理',
            component: () => import('@/views/student/index.vue'),
          },
          {
            path: 'detail',
            component: () => import('@/views/student/detail.vue'),
            name: '学生信息详情',
          },
          {
            path: 'add',
            component: () => import('@/views/student/add.vue'),
            name: '学生信息新增',
          },
          {
            path: 'batch',
            component: () => import('@/views/student/batchAdd.vue'),
            name: '学生信息批量新增',
          },
        ],
      },
      {
        path: '/major',
        component: () => import('@/views/major/index.vue'),
        name: '专业管理',
      },
      {
        path: '/teacher',
        name: '教师管理',
        children: [
          {
            path: '/teacher',
            name: '教师管理',
            component: () => import('@/views/teacher/index.vue'),
          },
          {
            path: 'detail',
            component: () => import('@/views/teacher/detail.vue'),
            name: '教师详情',
          },
          {
            path: 'add',
            component: () => import('@/views/teacher/add.vue'),
            name: '教师新增',
          },
        ],
      },
      {
        path: '/course',
        name: '课程管理',
        children: [
          {
            path: '/course',
            name: '课程管理',
            component: () => import('@/views/course/index.vue'),
          },
          {
            path: 'detail',
            component: () => import('@/views/course/detail.vue'),
            name: '课程详情',
          },
          {
            path: 'add',
            component: () => import('@/views/course/add.vue'),
            name: '课程新增',
          },
        ],
      },
      {
        path: '/setting',
        name: '系统设置',
        children: [
          {
            path: 'mycenter',
            component: () => import('@/views/setting/myCenter.vue'),
            name: '个人中心',
          },
          {
            path: 'changePassword',
            component: () => import('@/views/setting/changePassword.vue'),
            name: '修改密码',
          },
          {
            path: 'changeTheme',
            component: () => import('@/views/setting/changeTheme.vue'),
            name: '切换主题',
          },
          {
            path: 'log',
            component: () => import('@/views/setting/log.vue'),
            name: '系统日志',
          },
        ],
      },
    ],
  },

  // 404页面
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/index.vue'),
    name: '错误页面',
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach((to, from, next) => {
  const user = useUserStore()
  const token = user.token
  if (to.path === '/login') {
    next()
  } else {
    if (!token) {
      next('/login')
    }
    next()
  }
})
export default router
